/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author lab
 */
public class LastVisit extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    String UpdateNumberCookie(HttpServletRequest request, HttpServletResponse response) {
        if (request.getCookies() != null) {
            Cookie[] cookies = request.getCookies();
            //If vistied value already exist, increase it by 1
            boolean existed = false;
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals("number")) {
                    int num = Integer.parseInt(cookies[i].getValue());
                    num += 1;
                   
                    response.addCookie(new Cookie("number", String.valueOf(num)));
                    existed = true;
                    return  String.valueOf(num);
                }
               
            }
            //Else, it's mean first time visit, create the number cookie
            if (!existed) {
                Cookie c = new Cookie("number", "0");
                response.addCookie(c);
                return c.getValue();
            }

            //Find value of "number" cookie and return
            for (int i = 0; i < request.getCookies().length; i++) {
                Cookie c = request.getCookies()[i];
                if (c.getName().equals("number")) {
                    return c.getValue();
                }
            }

        } 
        else {
            Cookie c = new Cookie("number", "0");
            response.addCookie(c);
        }

        return "0";

    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //UpdateNumberCookie(request, response);
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet LastVisit</title>");
            out.println("</head>");
            out.println("<body>");
//            if (request.getCookies() != null) {
//                Cookie[] cookies = request.getCookies();
//                
//                for (int i = 0; i < cookies.length; i++) {
//                    if (cookies[i].getName().equals("visitted") && cookies[i].getValue().equals("true")) {
//                        
//                    }
//                }
//            };
            out.println("<table>");
            out.println("<tr>");
            out.println("<td>");
            out.println("Number of page last accessed");

            out.println("</td>");
            out.println("<td>");
            String  number = UpdateNumberCookie(request, response);
            out.print(number);
            out.println("</td>");
            
            out.println("</tr>");
            out.println("</table>");
            out.print("form");
            out.println("</body>");
            out.println("</html>");

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
